﻿using hygge_imaotai;
using hygge_imaotai.Domain;
using hygge_imaotai.Entity;
using hygge_imaotai.Repository;
using imaotai.Entity;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace imaotai.UserInterface.UserControls {
    /// <summary>
    /// ReservationResultControl.xaml 的交互逻辑
    /// </summary>
    public partial class ReservationResultControl : UserControl {
        private static readonly Logger Log = LogManager.GetCurrentClassLogger();

        public ReservationResultControl() {
            InitializeComponent();

            DataContext = new ReservationResultViewModel();
            QueryData();
        }

        private void QueryData() {
            var model = (ReservationResultViewModel)DataContext;
            ReservationResultViewModel.ReservationResultList.Clear();

            DB.Sqlite.Select<ReservationResultEntity>()
                .WhereIf(!string.IsNullOrEmpty(model.Phone),
                    i => i.MobilePhone.Contains(model.Phone))
                .WhereIf(!string.IsNullOrEmpty(model.Status),
                    i => i.Status.Contains(model.Status))
                .OrderByDescending(a => a.Id)
                .Count(out var count)
                .Page(1, 10)
                .ToList()
                .ForEach(ReservationResultViewModel.ReservationResultList.Add);

            // 分页数据
            var pageCount = count / 10 + 1;
            model.Total = count;
            model.PageCount = pageCount;
        }


        private void QueryButton_OnClick(object sender, RoutedEventArgs e) {
            QueryData();
        }

        private void ResetButton_OnClick(object sender, RoutedEventArgs e) {
            var model = (ReservationResultViewModel)DataContext;
            model.Phone = "";
            model.Status = "";
        }

        private void BtnClear_OnClick(object sender, RoutedEventArgs e) {
            var dr = MessageBox.Show("确定要删除所有数据吗？", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Asterisk);
            if (dr != MessageBoxResult.OK) return;

            var rows = DB.Sqlite.Delete<ReservationResultEntity>().Where("1=1").ExecuteAffrows();
            MessageBox.Show($"操作成功，共删除[{rows}]条数据。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            QueryData();
        }

        private void BtnQueryResult_OnClick(object sender, RoutedEventArgs e) {
            try {
                _ = IMTService.appointmentResults();
                MessageBox.Show("操作成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            } catch (Exception ex) {
                Log.Error(ex);
                MessageBox.Show(ex.Message, "出错啦", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
